<?php
/**
 * Created by PhpStorm.
 * User: C。
 * Date: 2019/8/13
 * Time: 9:04
 */
namespace app\home\controller;

use app\common\model\Member as MemberModel;
use app\common\model\Friends as FriendsModel;

/**
 * 好友控制器
 * Class Fgroup
 * @package app\home\controller
 */
class Friends extends Base
{
    /**
     * [getIndex  好友列表]
     * @param
     * @return json
     * @author [C。]
     */
    public function getIndex()
    {
        $param = input('param.');
        $frmodel = new FriendsModel();
        $mmodel = new MemberModel();
        $mem = $this->getMember($param['token']);
        $map = [];

        $this->getPageAndSize($param);
        try{
            $map['unino'] = ['like','%\_'.$mem['id'].'\_%'];
            $map['states'] = 1;
            $lists = $frmodel->getAllData($map,'id','memid,friend_id');

            $data = [];
            $i = 0;
            foreach($lists as $k=>$v){
               if($v['memid'] == $mem['id']){
                   $friend = $mmodel->findData($v['friend_id'],'id','username,img');
                   if(!empty($friend)){
                       $data[$i]['memid'] = $v['friend_id'];
                   }

               }else{
                   $friend = $mmodel->findData($v['memid'],'id','username,img');
                   if(!empty($friend)) {
                       $data[$i]['memid'] = $v['memid'];
                   }
               }
               if(!empty($friend)){
                   $http = substr($friend['img'],0,4);
                   if($http != 'http'){
                       $data[$i]['img'] = config('domain_name').$friend['img'];
                   }else{
                       $data[$i]['img'] = $friend['img'];
                   }
                   $data[$i]['username'] = $friend['username'];
                   $i++;
               }

            }
        }catch(\Exception $e){
            systembug($e->getMessage(),2,$this->url);
            return admin_json(-1, '获取失败'.$e->getMessage());
        }

        return admin_json(1, '获取成功', $data);
    }
    /**
     * [postAdd  添加好友]
     * @param
     * @return json
     * @author [C。]
     */
    public function postAdd()
    {
        $param = input('param.');
        $frmodel = new FriendsModel();
        $mem = $this->getMember($param['token']);
        if($mem['id'] == $param['friend_id']){
            return admin_json(3, '自己不能添加自己');
        }
        $map1['unino'] = '_'.$mem['id'].'_'.$param['friend_id'].'_';
        $map2['unino'] = '_'.$param['friend_id'].'_'.$mem['id'].'_';
        $friend = $frmodel->getOne($map1,$map2);
        if(!empty($friend)){
            return admin_json(2, '已经是好友了！');
        }
        try{
            $data = array(
                'memid'=>$mem['id'],
                'friend_id'=>$param['friend_id'],
                'unino'=>'_'.$mem['id'].'_'.$param['friend_id'].'_',
                'addtime'=>time()
            );
            $frmodel->addData($data);
        }catch(\Exception $e){
            systembug($e->getMessage(),2,$this->url);
            return admin_json(-1, '添加失败'.$e->getMessage());
        }
        return admin_json(1, '添加成功');
    }
    /**
     * [postAdd  添加好友]
     * @param
     * @return json
     * @author [C。]
     */
    public function postEdit()
    {
        $param = input('param.');
        $model = new FriendsModel();
        try{
            $model->editData(['id'=>$param['id'],'states'=>1]);
        }catch(\Exception $e){
            systembug($e->getMessage(),2,$this->url);
            return admin_json(-1, '添加好友未通过'.$e->getMessage());
        }
        return admin_json(1, '添加好友已通过');
    }
}